// 给index.html中的js和css加上v?=版本号
const fs = require('fs');
let index = `${__dirname}/index.html`;
var version = fs.readFileSync(`${__dirname}/version.js`, "utf-8");
version = version.toString();
let arr = version.split("=");
version = arr[1].replace(/\"/g, '')
console.log("开始替换版本号");
changeContent(index, '\\.js">', '.js' + "?v=" + version+'">');
changeContent(index, '\\.css"', '.css' + "?v=" + version+'"');
console.log("版本号替换成功:"+version);

function changeContent(_path, repStr, newStr = undefined) {
  var data = fs.readFileSync(_path, "utf-8");
  data = data.toString();
  var reg = new RegExp(repStr, "g")
  if (data.indexOf(newStr) == -1 || newStr === "") {
    data = data.replace(reg, newStr);
  }
  fs.writeFileSync(_path, data);
  return _path;
}